import datetime

from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.orm import relation, backref, mapper
from sqlalchemy.types import BigInteger, Integer, String, DateTime, Text, Boolean, Date
from sqlalchemy.dialects.mysql import ENUM

from fidoweb.model.meta import Base

"""
Rules
	1. Tables and Inner relations are sorted in alphabet order, and Inner relations are defined after Tables
	2. About relations:
		For m-m and o-o relations: 'relation' defined in the first element of the relation
		For o-m(m-o) relations: 'relation' defined in the element of 'm' side
"""

"""
Tables
	Media139Mail
	Media139MailTrialUser
	Media139MailType
"""

"""
Inner relations
	Media139Mail(type) o-m Media139MailType(mails)
"""

"""
Outer relations
	Media139MailTypes(users) m-m User(media139MailTypes) (Describing whether a user need a certain kind of Fido Media)
	Media139MailTrialUser(user) o-o User(media139MailTrialUser)
"""

Media139Mail_t = Table('media139Mails', Base.metadata,
	Column('id', BigInteger, primary_key = True),
	Column('createTime', DateTime, index = True),
	Column('content', Text),
	Column('type_id', Integer, ForeignKey('media139MailTypes.id'), index = True),
)

Media139MailTrialUser_t = Table('media139MailTrialUsers', Base.metadata,
	Column('id', BigInteger, ForeignKey('users.id'), primary_key = True),
)

Media139MailType_t = Table('media139MailTypes', Base.metadata,
	Column('id', Integer, primary_key = True),
	Column('name', String(64), unique = True),
)

Media139MailType_User_t = Table('media139MailTypes_users', Base.metadata,
	Column('media139MailType_id', Integer, ForeignKey('media139MailTypes.id'), primary_key = True),
	Column('user_id', BigInteger, ForeignKey('users.id'), primary_key = True),
	Column('createTime', DateTime(timezone = True), index = True),
)

class Media139Mail(object) :
	def __init__(self, content, type_id) :
		self.createTime = datetime.datetime.now()
		self.content = content
		self.type_id = type_id
		
class Media139MailTrialUser(object) :
	def __init__(self, user_id) :
		self.id = user_id

class Media139MailType(object) :
	def __init__(self, name) :
		self.name = name
		
class Media139MailType_User(object) :
	def __init__(self, media139MailType_id, user_id) :
		self.media139MailType_id = media139MailType_id
		self.user_id = user_id
		self.createTime = datetime.datetime.now()

from fidoweb.model.user import User
mapper(Media139Mail, Media139Mail_t, properties = {
	'id' : Media139Mail_t.c.id,
	'createTime' : Media139Mail_t.c.createTime,
	'content' : Media139Mail_t.c.content,
	'type_id' : Media139Mail_t.c.type_id,
})

mapper(Media139MailTrialUser, Media139MailTrialUser_t, properties = {
	'id' : Media139MailTrialUser_t.c.id,
	'user' : relation(User, backref = 'media139MailTrialUser'),
})

mapper(Media139MailType, Media139MailType_t, properties = {
	'id' : Media139MailType_t.c.id,
	'name' : Media139MailType_t.c.name,
	'mails' : relation(Media139Mail, backref = 'type'),
	'users' : relation(User, secondary = Media139MailType_User_t, backref = 'media139MailTypes')
})

mapper(Media139MailType_User, Media139MailType_User_t, properties = {
	'media139MailType_id' : Media139MailType_User_t.c.media139MailType_id,
	'user_id' : Media139MailType_User_t.c.user_id,
	'createTime' : Media139MailType_User_t.c.createTime
})